package com.yzx.codedemo.util;

import com.yzx.codedemo.entity.Person;
import com.yzx.codedemo.entity.Student;
import com.yzx.codedemo.enums.AgeRangeEnum;

import javax.management.BadAttributeValueExpException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 第一天Stream问题解决
 *
 * @author 伦裕如
 * @since 2024-04-16
 */
public class PracticesUtils {


    public static List<String> practice1(List<String> stringList) {

        // 使用Stream流对数据过滤（长度大于等于5），然后使用map方法对元素进行转换操作
        return stringList.stream().filter(s -> s.length() >= 5)
                .map(String::toUpperCase).collect(Collectors.toList());

    }


    public static Map<Integer, Long> practice2(List<String> stringList) {

        // 发现Stream流提供有分组方法，直接调用方法完成分组操作了
        return stringList.stream().collect(Collectors.groupingBy(String::length, Collectors.counting()));
    }


    public static void practice3(List<Integer> integerList) {

        // 获取最大值
        Optional<Integer> max = integerList.stream().max(Comparator.comparingInt(o -> o));
        System.out.println("max: " + max.get());

        // 获取最小值
        Optional<Integer> min = integerList.stream().min(Comparator.comparingInt(o -> o));
        System.out.println("min: " + min.get());
    }


    public static List<Integer> practice4(List<List<Integer>> list) {

        // fixme
        return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
    }


    public static List<String> practice5(String[] strings) {

        // 过滤元素
        return Arrays.stream(strings)
                // 长度大于 3
                .filter(s -> s.length() > 3 && s.contains("a"))
                .collect(Collectors.toList());
    }


    public static int[] practice6(int[] nums) {

        return Arrays.stream(nums).distinct().toArray();
    }


    public static Map<String, List<Integer>> practice7(Integer[] integers) {

        return Arrays.stream(integers)
                .collect(Collectors.groupingBy(integer -> integer % 2 != 0 ? "奇数" : "偶数"));
    }


    public static int practice8(List<Integer> integerList) {

        return integerList.stream().filter(integer -> integer % 2 == 0)
                .reduce(0, (sum, integer) -> sum + integer * integer);
    }


    public static Map<Object, List<Person>> practice9(List<Person> list) {

        return list.stream().collect(Collectors
                .groupingBy(person -> person.getSex().getName() + "#" + person.getAge()));
    }


    public static List<String> practice10(List<String> stringList) {
        return stringList.stream().sorted((o1, o2) -> o2.length() - o1.length())
                .limit(3).collect(Collectors.toList());
    }


    public static void practice11(List<Person> personList) {

        Map<AgeRangeEnum, Long> map = personList.stream().collect(Collectors
                .groupingBy(Person::judgeAgeRange, Collectors.counting()));
        map.forEach((ageRangeEnum, count) -> System.out.println(ageRangeEnum.getName() + " " + count) );

    }

    public static long practice12(Double[] scores) throws BadAttributeValueExpException {

        Double average = Arrays.stream(scores).collect(Collectors.averagingDouble(s -> s));
        System.out.println(average);
        // 用filter过滤出大于平均分的成绩，然后用count()求人数
        return Stream.of(scores).filter(s -> s > average).count();
    }

    public static Map<String, List<Student>> practice13(List<Student> studentList) {

        return studentList.stream().collect(Collectors
                .groupingBy(Student::getClassroom,
                        HashMap::new,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                students -> students.stream().sorted().collect(Collectors.toList()))));
    }
}
